<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <el-table :data="cateList" style="width: 100%">
        <el-table-column label="序号" type="index" width="180">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180">
        </el-table-column>
        <el-table-column label="分类别名" width="180">
          <template v-slot='scope'>
            <!-- scope的名字是自定义的,但是scope中的row属性数element-ui规定好的 -->
            <!-- scope.row表示每一行数据 -->
            <!-- 这样写的目的是:利用子组件的数据scope.row定制它的默认模板 -->
            <span :style="{color:'red'}">{{scope.row.cate_alias}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- 作用域插槽scope.row表示每一行数据 -->
            <!-- 标签之间的内容定制列的具体内容 -->
            <el-button type="primary" size="mini" @click="editBtn(scope.row.id)">修改</el-button>
            <el-button type="danger" size="mini" @click="delBtn(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="添加文章分类" :visible.sync="addVisible" width="40%" @closed="$refs.addRef.resetFields()">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">添加</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑文章分类" :visible.sync="editVisible" width="30%">
      <!-- 添加的表单 -->
      <el-form :model="editForm" :rules="addRules" ref="editRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      // 控制添加对话框的显示与隐藏
      addVisible: false,
      // 修改表单的数据对象
      editForm: {},
      // 控制修改对话框的显示与隐藏
      editVisible: false,
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 初始化获取文章分类
    async initCateList () {
      const res = await this.$http.get('/my/cate/list')
      if (res.data.code === 0) {
        this.cateList = res.data.data
      }
    },
    addCate () {
      // 表单校验
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        const res = await this.$http.post('/my/cate/add', this.addForm)
        if (res.data.code === 1) this.$message.error('添加失败')
        this.$message.success('添加成功')
        // 关闭对话框
        this.addVisible = false
        // 重新请求列表数据
        this.initCateList()
      })
    },
    // 编辑文章分类功能
    async editBtn (id) {
      //  1. 获取分类的基本信息
      const res = await this.$http.get('/my/cate/info', {
        params: { id }
      })
      if (res.data.code === 0) {
        // 2. 为 data 中的 editForm 赋值
        this.editForm = res.data.data
        // 3. 展示对话框
        this.editVisible = true
      }
    },
    editCate () {
      this.$refs.editRef.validate(async valid => {
        if (!valid) return
        const res = await this.$http.put('/my/cate/info', this.editForm)
        if (res.data.code === 0) {
          this.editVisible = false
          this.initCateList()
        }
      })
    },
    // 删除文章分类
    async delBtn (id) {
      // 不允许删除前两个分类
      try {
        if (id === 1 || id === 2) {
          return this.$message.error('管理员不允许删除这个分类！')
        }
        await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        const res = await this.$http.delete('/my/cate/del', {
          params: { id }
        })
        if (res.data.code === 1) return this.$message.error('删除分类失败！')
        if (res.data.code === 0) {
          this.$message.success('删除分类成功！')
          this.initCateList()
        }
      } catch (e) {
        if (e === 'cancel') {
          console.log('点击了取消按钮')
        } else {
          this.$message.error('删除分类失败！')
        }
      }
      // try{}catch{}异常处理
      // 1.可以保证后续代码的执行
      // 2. 可以监听到代码的错误
      // 3. promise异常处理通过catch方法捕获 p.then().catch()
      // 4. async/await的异常处理需要采用try catch结构进行处理
      // catch里一般仅处理错误提示即可
    }

  },
  created () {
    this.initCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
